using Godot;
using System;

public partial class SkillSelectionItem : MarginContainer
{
	[Signal]
	public delegate void OnSkillSelectEventHandler(BaseSkill skill);

	TextureRect icon;
	Label labelCost;
	Label skillName;
	Label skillDescription;
	UIButton uiButton;

	static readonly Color okColor = Color.FromHtml("#00cfff");
	static readonly Color errColor = Color.FromHtml("#ff0000");

	public override void _Ready()
	{
		base._Ready();
		icon = GetNode<TextureRect>("%Icon");
		labelCost = GetNode<Label>("%LabelCost");
		skillName = GetNode<Label>("%SkillName");
		skillDescription = GetNode<Label>("%SkillDescription");
		uiButton = GetNode<UIButton>("%UIButton");
		uiButton.OnPressed += OnSkillSelcectHandler;
	}

	private void OnSkillSelcectHandler(UIButton button)
	{
		EmitSignal(SignalName.OnSkillSelect, skill);
	}

	BaseSkill skill;
	public void Init(BaseSkill sk)
	{
		skill = sk;
		// icon.Texture = GameUtil.GetBuffIcon
		labelCost.Text = sk.Cost[SkillCostType.Sprite].ToString();
		var color = okColor;
		if (sk.Cost.ContainsKey(SkillCostType.Health) && sk.OwnerRobot.Entity.CurrentHealth < sk.Cost[SkillCostType.Health].As<int>())
		{
			color = errColor;
		}
		else if (sk.Cost.ContainsKey(SkillCostType.Sprite) && sk.OwnerRobot.Entity.CurrentSprite < sk.Cost[SkillCostType.Sprite].As<int>())
		{
			color = errColor;
		}
		labelCost.AddThemeColorOverride("font_color", color);
		skillName.Text = sk.SkillName;
		skillDescription.Text = sk.SkillDescription;
	}
}
